{
  class Node {
    constructor(public element: any = null, public next: Node | null = null) {}
  }
  // let n2 = new Node("world");
  // let n1 = new Node("hello", n2);
  // console.log(n1.element, n1.next);

  class LinkList<T> {
    public head = new Node("head");
    find(item: T): Node {
      let currNode = this.head;
      while (currNode.next) {
        if (currNode.element === item) {
          break;
        } else {
          currNode = currNode.next;
        }
      }
      return currNode;
    }
    findPrevious(item: T): Node {
      // 查找一个元素的前一个元素
      let currNode = this.head;
      while (currNode.next) {
        if (currNode.next.element === item) {
          break;
        } else {
          currNode = currNode.next;
        }
      }
      return currNode;
    }
    insert(newElement: T, item: T): void {
      let node = new Node(newElement);
      let currNode = this.find(item);
      node.next = currNode.next;
      currNode.next = node;
    }
    remove(item: T): void {
      let prevNode = this.findPrevious(item);
      if (prevNode.next) {
        prevNode.next = prevNode.next.next;
      }
    }
    display(): void {
      let currNode = this.head;
      while (currNode) {
        console.log(currNode.element);
        if (currNode.next) {
          currNode = currNode.next;
        } else {
          break;
        }
      }
    }
  }

  let list = new LinkList<string>();
  list.insert("Jimmy", "head");
  list.insert("xuexue", "jimmy");
  list.insert("Henry", "xuexue");
  list.insert("Jack", "Henry");
  list.display();
  list.remove("Jack");
  console.log("---------");
  list.display();
}
